local json = require "json"
local net = require "net"

local M = {}

local RPC = {}

function RPC.c2s_add(fd, proto)
    print("c2s_add:", proto.a, proto.b)
    local ret = proto.a + proto.b
    return {
        pid = "s2c_add",
        ret = ret,
    }
end

function RPC.c2s_sub(fd, proto)
    print("c2s_sub:", proto.a, proto.b)
    local ret = proto.a - proto.b
    return {
        pid = "s2c_sub",
        ret = ret,
    }
end

function M.handle_proto(fd, json_proto)
    local proto = json.decode(json_proto)
    local pid = proto.pid
    local func = RPC[pid]
    if not func then
        print("Unkonw pid:", pid)
        return
    end

    local r = func(fd, proto)
    net.send_to_client(fd, json.encode(r))
end

return M


{
  "pid": "c2s_sub",
  "a": 1,
  "b": 1
}
{
  "pid": "s2c_sub",
  "ret": 1
}


{
  "pid": "c2s_sub",
  "a": 1,
  "b": 1
}
{
  "pid": "s2c_sub",
  "ret": 1
}